package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import ch.rheintalconnection.sysventory.dto.PrinterConfigurationDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class PrinterConfigurationScan {
	
	public static final String QUERY = "SELECT * FROM Win32_PrinterConfiguration";
	
	private ActiveXComponent axWMI;
	private String host;
	
	private static Logger logger = LogManager.getLogger(PrinterConfigurationScan.class);
	
	public PrinterConfigurationScan(ActiveXComponent axWMI, String host) {
		super();
		this.axWMI = axWMI;
		this.host = host;
	}



	public List<PrinterConfigurationDto> scan(){
		
		ArrayList<PrinterConfigurationDto> printerConfigurationDtos = new ArrayList<PrinterConfigurationDto>();
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		PrinterConfigurationDto printerConfigurationDto;
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();

			String version = Dispatch.call(item, "DriverVersion").toString();
			
			printerConfigurationDto = new PrinterConfigurationDto(version);
			printerConfigurationDtos.add(printerConfigurationDto);
			
			logger.debug(host + " scanned " + printerConfigurationDto);
		}
		
		return printerConfigurationDtos;	
	}
		
}
